﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using CellarDoor.Gui.Theming;
using CellarDoor.Gui.Layout;

namespace CellarDoor.Gui.Elements
{
    public class UiCheckBox : UiButton
    {       
        private bool isChecked = false;
        /// <summary>
        /// Checkes or uncheckes the box.
        /// </summary>
        public bool Checked
        {
            get { return isChecked; }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    OnCheckedChanged();
                }
            }
        }

        /// <summary>
        /// Occurs when check box checked or unchecked
        /// </summary>
        public event EventHandler CheckedChanged;

        /// <summary>
        /// Creates a check box control.
        /// </summary>
        public UiCheckBox()
            : base()
        {
            Initialize();
        }

        /// <summary>
        /// Creates a check box control.
        /// </summary>
        public UiCheckBox(UiElement parent)
            : base(parent)
        {
            Initialize();
        }

        private void Initialize()
        {
            base.Style = new UiVisualStyleChecked(this);
            Margin = new UiPadding(3);
            Padding = new UiPadding(3);
            TextAlign = UiAlignStyle.Middle | UiAlignStyle.OutsideRight;
            ElementName = "Checkbox";
            Width = 30;
            Height = 30;
        }

        protected override void OnMouseClick(UiMouseProxy mouse)
        {
            Checked = !isChecked;
            base.OnMouseClick(mouse);
        }

        private void OnCheckedChanged()
        {
            if (Style is UiVisualStyleChecked)
            {
                (Style as UiVisualStyleChecked).CheckedLayer.Enabled = Checked;
            }
            if (CheckedChanged != null)
            {
                CheckedChanged(this, EventArgs.Empty);
            }
        }
    }
}
